package de.baumann.browser.activitys.user

import de.baumann.browser.R
import de.baumann.browser.base.BaseOdinActivity
import de.baumann.browser.iview.IAccountView
import de.baumann.browser.present.AccountPasswordPresenter
import de.baumann.browser.units.TimerUnit
import de.baumann.browser.views.dialog.Callback
import de.baumann.browser.views.dialog.VaptchaDialog
import kotlinx.android.synthetic.main.activity_retrieve_password.*
import kotlinx.android.synthetic.main.include_tool_bar.*

/**
 *@author:
 *@date:2018/12/25 19:24
 *@discription: 重置密码
 */
class RetrievePasswordActivity : BaseOdinActivity<IAccountView, AccountPasswordPresenter>(), IAccountView {

    private var timer: TimerUnit? = null

    override fun getLayout(): Int {
        return R.layout.activity_retrieve_password
    }

    override fun initView() {
        initToolbar(commToolBar, null)
        tvGetSmsCode.setOnClickListener {
            VaptchaDialog(mContext,object :Callback{
                override fun callback(token: String) {
                    runOnUiThread {
                        timer = TimerUnit(tvGetSmsCode)
                        timer?.startTime()
                        presenter.retrieveGetSmsCode(token)
                    }
                }
            }).show()
        }

        btnRetrive.setOnClickListener {
            presenter.resetPassword()
        }
    }

    override fun createPresenter(): AccountPasswordPresenter {
        return AccountPasswordPresenter()
    }

    override fun createView(): IAccountView {
        return this
    }

    override fun onError(errorMsgId: Int) {
        super.onError(errorMsgId)
        endTimer()
    }

    override fun onError(errorMsg: String?) {
        super.onError(errorMsg)
        endTimer()
    }

    private fun endTimer() {
        if (timer != null)
            timer!!.endtTime()
    }

    override fun initData() {

    }

    override val password: String
        get() = edRetrivePassword.text.trim().toString()
    override val otherCode: String
        get() = edRepeatPassword.text.trim().toString()


    override val telNum: String
        get() = edRetriveTel.text.trim().toString()
    override val smsCode: String
        get() = edRetriveSmsCode.text.trim().toString()


    override fun requestSuccess() {
        showShortToast(getString(R.string.password_reset_complete))
        finish()
    }

    override fun smsCodeSent() {
        showShortToast("验证码已发送")
    }

    override fun showBindEth(): Boolean {
        return false
    }

}
